﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.DisableArea.DisableAreaVehicleTypeReference;
using Dapper;

namespace BCData.TMS.DisableArea.DisableAreaVehicleTypeReference
{
    public class DisableAreaVehicleTypeReferenceData : IDisableAreaVehicleTypeReferenceData
    {
        private readonly IDatabaseContext database;

        public DisableAreaVehicleTypeReferenceData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<int> Add(long disableAreaId, List<DisableAreaVehicleTypeReferenceEntity> disableAreaVehicleTypeReferenceEntities)
        {
            int i = 0;
            foreach (var item in disableAreaVehicleTypeReferenceEntities)
            {
                item.DisableAreaId = disableAreaId;
                //sqls.Add(GetInsertSql(item, out DynamicParameters parameter));
                string sql = GetInsertSql(item, out DynamicParameters parameter);
                //parameters.Add(parameter);
                i += await database.Master.ExecuteAsync(sql, parameter);
            }

            return i;
        }

        private string GetInsertSql(DisableAreaVehicleTypeReferenceEntity entity, out DynamicParameters parameters)
        {
            parameters = new DynamicParameters();
            string sql = SqlCommands.GetInsertSql("DisableAreaVehicleTypeReference", new string[] { "DisableAreaId", "VehicleTypeId" });
            parameters.Add("?DisableAreaId", entity.DisableAreaId, DbType.Int64, size: 20);
            parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32);
            return sql;
        }

        public bool Delete(long disableAreaId)
        {
            DynamicParameters parameters = new DynamicParameters();
            string DELETE_SQL = @"DELETE FROM DisableAreaVehicleTypeReference WHERE DisableAreaId=?DisableAreaId";
            parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64, size: 20);

            //return this.sqlCommand.Execute(DELETE_SQL, CommandType.Text, parameters) > 0;
            return database.Master.Execute(DELETE_SQL, parameters) > 0;
        }

        public async Task<IEnumerable<DisableAreaVehicleTypeReferenceEntity>> QueryDisableAreaVehicleTypeReference(long disableAreaId)
        {
            DynamicParameters parameters = new DynamicParameters();

            string QUERY = @"SELECT A.DisableAreaVehicleTypeReferenceId,A.DisableAreaId,A.VehicleTypeId,B.VehicleTypeName
                            FROM DisableAreaVehicleTypeReference A
                                INNER JOIN VehicleType B ON A.VehicleTypeId=B.VehicleTypeId
                            WHERE DisableAreaId=?DisableAreaId";
            parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64, size: 20);

            //return this.sqlCommand.Query<DisableAreaVehicleTypeReferenceEntity>(QUERY, CommandType.Text, parameters);
            return await database.Slave.QueryAsync<DisableAreaVehicleTypeReferenceEntity>(QUERY, parameters);
        }
    }
}
